#ifndef SEQUENCERUNNER_H
#define SEQUENCERUNNER_H

#include "IF_SequenceEngineInterface.h"
#include "sequenceitem.h"
#include "ITimerService.h"

class QAbstractListModel;


class SequenceRunner : public IF_SequenceEngineInterface
{
   Q_OBJECT
public:
   explicit SequenceRunner(const QAbstractListModel &sequenceModel, ITimerService *timer,
                           QObject *parent = 0);

   void play();
   void pause();
   bool isPlaying();

public slots:
   /**
    * pause the sequence and set item given item (\p index) as the
    * one that will be immediately fired on next play.
    * @param index - must belong to sequence model
    */
   void selectIndex( const QModelIndex &index);
   void selectIndex( int rowIndex);

signals:
   void activateLight(const QString &, bool);
   void activateMedia(const QString &, bool);
   void activatePicture(const QString &, bool);
   void activateRow(int);
   void sequenceIsOver();
   void timeToNextEvent(int);

private:
   bool m_isPlaying;
   const QAbstractListModel & m_sequenceModel;
   ITimerService *m_timer;
   TimeoutListener m_timerlistener;

   /** index of item in sequence model that will be triggered
     * on next 'play' command or timer expiration */
   int m_indexOfArmed;

   /** index of last item that has been triggered while playing sequence.
    * This value is -1 if sequence is paused
    */
   int m_indexOfPlaying;

private slots:
   void onReadyToPlayNext();

private:
   void updateArmedIndex();
   void requestActivation(SequenceItem::eventType type, QString label);
   void armNextTimeout();
   bool armedItemIsValid();
};

#endif // SEQUENCERUNNER_H
